﻿// 
//    Copyright 2010 The JaernCloud Project
// 
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
// 
//        http://www.apache.org/licenses/LICENSE-2.0
// 
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// 
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using JaernCloud.Language.Compiler.Ast;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Text.Tagging;
using Microsoft.VisualStudio.Utilities;

namespace JaernCloud.VisualStudio.Classification {

    [Export(typeof(ITaggerProvider))]
    [ContentType("JaernCloud")]
    [TagType(typeof(ClassificationTag))]
    internal sealed class JaernCloudClassifierProvider : ITaggerProvider {
        [Export]
        [Name("JaernCloud")]
        [BaseDefinition("code")]
        internal static ContentTypeDefinition JaernCloudContentType = null;

        [Export]
        [FileExtension(".jcl")]
        [ContentType("JaernCloud")]
        internal static FileExtensionToContentTypeDefinition JaernCloudFileType = null;

        [Import]
        internal IClassificationTypeRegistryService ClassificationTypeRegistry = null;

        [Import]
        internal IBufferTagAggregatorFactoryService aggregatorFactory = null;

        public ITagger<T> CreateTagger<T>(ITextBuffer buffer) where T : ITag {
            ITagAggregator<JaernCloudTokenTag> jcTagAggregator = aggregatorFactory.CreateTagAggregator<JaernCloudTokenTag>(buffer);
            return new JaernCloudClassifier(buffer, jcTagAggregator, ClassificationTypeRegistry) as ITagger<T>;
        }
    }

    internal sealed class JaernCloudClassifier : ITagger<ClassificationTag> {
        ITextBuffer buffer;
        ITagAggregator<JaernCloudTokenTag> jcTagAggregator;
        IDictionary<AstNodeKind, IClassificationType> jcTypes;

        internal JaernCloudClassifier(ITextBuffer buffer, ITagAggregator<JaernCloudTokenTag> jcTagAggregator, IClassificationTypeRegistryService typeService) {
            this.buffer = buffer;
            this.jcTagAggregator = jcTagAggregator;
            jcTypes = new Dictionary<AstNodeKind, IClassificationType>();
            foreach (AstNodeKind astNodeKind in Enum.GetValues(typeof(AstNodeKind))) {
                jcTypes[astNodeKind] = typeService.GetClassificationType("JaernCloud_" + astNodeKind.ToString());
            }
        }

        public event EventHandler<SnapshotSpanEventArgs> TagsChanged {
            add { }
            remove { }
        }

        public IEnumerable<ITagSpan<ClassificationTag>> GetTags(NormalizedSnapshotSpanCollection spans) {

            foreach (var tagSpan in jcTagAggregator.GetTags(spans)) {
                var tagSpans = tagSpan.Span.GetSpans(spans[0].Snapshot);
                yield return
                    new TagSpan<ClassificationTag>(tagSpans[0], new ClassificationTag(jcTypes[tagSpan.Tag.Type]));
            }
        }
    }
}
